package users

import (
	"net/http"
	"product/common/enumeration"
	"product/common/utils"
	"product/common/validator"

	"github.com/zeromicro/go-zero/rest/httpx"
	"product/authorization/serve/internal/logic/users"
	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"
)

// LoginUsersHandler Login users
func LoginUsersHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.LoginUserReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := users.NewLoginUsersLogic(r.Context(), svcCtx)
		resp, err := l.LoginUsers(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		if validator.IsEmptyString(resp.Token) || validator.IsEmptyString(resp.ID) {
			httpx.OkJsonCtx(r.Context(), w, utils.Fail("登录失败"))
			return
		}
		httpx.OkJsonCtx(r.Context(), w, utils.Success(resp, "登录成功", enumeration.ProcessingSuccess))
	}
}
